/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using DaveSexton.DocProject.Engine;
using DaveSexton.DocProject.Resources;
using System.Globalization;

namespace DaveSexton.DocProject
{
	public sealed class DocProjectFile : DocProjectItemBase
	{
		#region Public Properties
		public override SystemPath Path
		{
			get
			{
				return file;
			}
		}

		public override bool Exists
		{
			get
			{
				return file.Exists;
			}
		}
		#endregion

		#region Private / Protected
		private readonly SystemPath file;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DocProjectFile" /> class.
		/// </summary>
		/// <param name="path">Absolute or relative path from the DocProject's configured <see cref="BuildSettings.TargetDirectory" />, including the file name.</param>
		/// <param name="missingBuildTypeDependency">Indicates the <see cref="BuildType" /> that can be executed to replace the item when the item is missing.</param>
		/// <param name="updatedBuildTypeDependency">Indicates the <see cref="BuildType" /> that is executed when the item has changed.</param>
		public DocProjectFile(SystemPath file, bool removeOnClean, bool includeInProject, 
			MissingBuildTypeDependency missingBuildTypeDependency, UpdatedBuildTypeDependency updatedBuildTypeDependency, bool isRequiredOutput)
			: base(removeOnClean, includeInProject, missingBuildTypeDependency, updatedBuildTypeDependency, isRequiredOutput)
		{
			if (file == SystemPath.Empty)
				throw new ArgumentException(Resources.Errors.EmptySystemPathIsInvalid, "file");

			if (file.IsDirectory || file.HasWildcard)
				throw new ArgumentException(Resources.Errors.SystemPathNotFile, "file");

			this.file = file;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="DocProjectFile" /> class.
		/// </summary>
		/// <param name="path">Absolute or relative path from the DocProject's configured <see cref="BuildSettings.TargetDirectory" />, including the file name.</param>
		/// <param name="missingBuildTypeDependency">Indicates the <see cref="BuildType" /> that can be executed to replace the item when the item is missing.</param>
		/// <param name="updatedBuildTypeDependency">Indicates the <see cref="BuildType" /> that is executed when the item has changed.</param>
		public DocProjectFile(SystemPath file, bool removeOnClean, bool includeInProject, 
			MissingBuildTypeDependency missingBuildTypeDependency, UpdatedBuildTypeDependency updatedBuildTypeDependency)
			: base(removeOnClean, includeInProject, missingBuildTypeDependency, updatedBuildTypeDependency)
		{
			if (file == SystemPath.Empty)
				throw new ArgumentException(Resources.Errors.EmptySystemPathIsInvalid, "file");

			if (file.IsDirectory || file.HasWildcard)
				throw new ArgumentException(Resources.Errors.SystemPathNotFile, "file");

			this.file = file;
		}
		#endregion

		#region Methods
		public override bool Delete(IAnyProject project)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			try
			{
				project.GetItem(file.Path).Delete();
				return true;
			}
			catch (UnauthorizedAccessException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, file, ex.Message);
			}
			catch (PathTooLongException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, file, ex.Message);
			}
			catch (NotSupportedException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, file, ex.Message);
			}
			catch (DirectoryNotFoundException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, file, ex.Message);
			}
			catch (IOException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, file, ex.Message);
			}

			return false;
		}
		#endregion
	}
}
